package com.ostrich.xflowlayoutdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.ostrich.xflowlayoutdemo.room.AppDataBase;
import com.ostrich.xflowlayoutdemo.room.HistoryDao;
import com.ostrich.xflowlayoutdemo.room.HistoryEntity;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity {
    private SearchView searchView;
    private TagFlowLayout idFlowlayout;

    private HistoryDao mHistoryDao;

    private  List<HistoryEntity> list = new ArrayList<>();
    private TagAdapter<HistoryEntity> adapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        searchView = (SearchView) findViewById(R.id.search_view);
        idFlowlayout = (TagFlowLayout) findViewById(R.id.id_flowlayout);
        mHistoryDao = AppDataBase.getInstance().getHistoryDao();
        adapter = new TagAdapter<HistoryEntity>(list) {
            @Override
            public View getView(FlowLayout parent, int position, HistoryEntity o) {
                TextView view = (TextView) LayoutInflater.from(MainActivity.this).inflate(R.layout.tv, parent, false);
                view.setText(o.getText());
                return view;
            }
        };
        idFlowlayout.setAdapter(adapter);

        query();

        idFlowlayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                //访问查询接口 查询数据展示数据
                return true;
            }
        });




    }

    private void query() {
        //数据库操作子线程
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                //查询历史记录
                List<HistoryEntity> historyEntities = mHistoryDao.selectAll();
                list.clear();
                list.addAll(historyEntities);
                //展示历史记录
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataChanged();
                    }
                });

            }
        });
    }

    public void serach(View view) {
        //数据库操作子线程
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                String s = searchView.getQuery().toString();
                HistoryEntity historyEntity = new HistoryEntity(0, s);
                mHistoryDao.insert(historyEntity);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list.add(historyEntity);
                        adapter.notifyDataChanged();

                    }
                });
            }
        });



    }

    public void clear(View view) {
        Executors.newCachedThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                mHistoryDao.clear();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        list.clear();
                        adapter.notifyDataChanged();
                    }
                });
            }
        });
    }
}